/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import net.worlds.WorldsException;
import net.worlds.ObjectList;
import net.worlds.Util;
import org.jbox2d.common.Vec2D;


public class PropsTableListener implements TableModelListener{

    public void tableChanged(TableModelEvent e) {
        if(e.getColumn() == 1)
        {
            String k = (String) ((TableModel) e.getSource()).getValueAt(e.getFirstRow(), 0);
            Object v = ((TableModel) e.getSource()).getValueAt(e.getFirstRow(), 1);
            if(v instanceof String)
            {
                String s = (String) v;
                if(Util.isInteger(s) && ObjectList.getPropDef(UI.getSelected().get(0).getClass().getSimpleName(), k).type == Float.class)
                    v = (float) Integer.parseInt(s);
                else if(Util.isInteger(s))
                    v = Integer.parseInt(s);
                else if(Util.isDouble(s))
                    v = Float.parseFloat(s);
                else if(Util.isColor(s))
                {
                    try {
                        v = Util.colorFromString(s);
                    } catch (WorldsException ex) {
                    }
                }
                else if(Util.isVec2D(s))
                {
                    v = new Vec2D(s);
                }
            }
            UI.doPropChange(k, v);
        }
    }

}
